package com.block.common.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.block.common.service.IAppLoginInfoService;
import com.block.db.entity.AppLoginInfo;
import com.block.db.entity.ApplicationInfo;
import com.block.db.mapper.AppLoginInfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * app 快捷登录 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-03-20
 */
@Service
public class AppLoginInfoServiceImpl extends ServiceImpl<AppLoginInfoMapper, AppLoginInfo> implements IAppLoginInfoService {

    @Resource
    private RedisTemplate<String, AppLoginInfo> redisAppLoginInfoTemplate;

    private  static  final  String APPLOGININFO_KEY_PREFIX="APPLOGININFO:APPTYPE:";
    private  static  final  Long APPLOGININFO_KEY_TIME_OUT=4L;

    @Override
    public AppLoginInfo getEntityInfoByAppCode(String appCode){

        String redisKey = APPLOGININFO_KEY_PREFIX+appCode;
        AppLoginInfo appLoginInfo= redisAppLoginInfoTemplate.opsForValue().get(redisKey);
        if(ObjectUtil.isEmpty(appLoginInfo)){
            appLoginInfo = this.baseMapper.getLoginInfoByCode(appCode);
            if(appLoginInfo!=null) {
                redisAppLoginInfoTemplate.opsForValue().set(redisKey, appLoginInfo, APPLOGININFO_KEY_TIME_OUT, TimeUnit.HOURS);
            }
        }
        return appLoginInfo;
    }


}
